<?php
namespace App\Http\Controllers\desktop\cs;

use Illuminate\Support\Facades\Input;
use App\Models\Bookshelf;
use App\Models\Book;
use Illuminate\Support\Facades\Redirect;

class BookshelfController extends BaseController {
	
	public function index() {
		$member = $this->getMember();
		$bookshelves = Bookshelf::where('member_id', $member->id)
				->orderBy('id', 'desc')
				->paginate(8);
		return view('desktop.cs.bookshelf', compact('bookshelves'));
	}
	
	public function store() {
		$book = Book::find(Input::get('bookId'));
		if (is_null($book)) {
			return redirect('/404');
		}
		$member = $this->getMember();
		$bookshelf = Bookshelf::where('member_id', $member->id)
				->where('book_id', $book->id)
				->first();
		if(!is_null($bookshelf)) {
			return Redirect::back();
		}
		
		Bookshelf::create([
			'book_id' => $book->id,
			'member_id' => $member->id,
		]);
		return redirect('/books/'.$book->id);
	}
	
	public function destroy($id) {
		$bookshelf = Bookshelf::find($id);
		if(!is_null($bookshelf)) {
			$bookshelf->delete();
		}
		return redirect('/cs/bookshelves');
	}
}
